package com.wanshi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanshi.entity.User;
import com.wanshi.service.impl.UserServiceImpl;
import com.wanshi.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-12-09
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
@ResponseBody
@Api(value = "测试SwaggerAPI Annotation", tags = {"测试SwaggerAPI"})
public class UserController {

    @Autowired
    public UserServiceImpl userService;

    @ApiOperation("列表展示")
    @GetMapping("/list")
    public Result findAll(){
        return Result.ok(userService.list());
    }

    @ApiOperation(value = "一个参数的测试", notes = "一个参数的测试")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name="id", value="id", dataTypeClass = Integer.class, required = true)
            }
    )
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        return Result.ok(userService.getById(id));
    }

    @GetMapping("/name/{name}")
    public List<User> findByName(@PathVariable String username){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("username", username);
        return userService.list(wrapper);
    }

    @GetMapping("/ids")
    public List<User> ids(@RequestBody List<String> id){
        return userService.listByIds(id);
    }

    @GetMapping("/c/cound")
    public List cound(@RequestBody User user){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(user.getId())){
            wrapper.eq("id", user.getId());
        }
        if(!StringUtils.isEmpty(user.getUsername())){
            wrapper.like("username", user.getUsername());
        }
        return userService.list(wrapper);
    }

    @GetMapping("/page/{ye}/{size}")
    public Result findPage(@PathVariable int ye, @PathVariable int size){
        Page<User> page = new Page<>(ye,size);
        userService.page(page);
        return Result.ok(page);
    }
    @ApiOperation(value = "三个参数的测试", notes = "三个参数的测试")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name="ye", value="页数", paramType = "path",required = true),
                    @ApiImplicitParam(name="size", value="一页显示多少条", paramType = "path", required = true),
            }
    )
    @PostMapping("/findPageAndCount/{ye}/{size}")
    public Result findPageAndCount(@PathVariable int ye,@PathVariable int size,@RequestBody User user){
        Page page = new Page(ye,size);
        QueryWrapper<User> wrapper = new QueryWrapper();
        if(!StringUtils.isEmpty(user.getUsername())){
            wrapper.like("username", user.getUsername());
        }
        if(!StringUtils.isEmpty(user.getCity())){
            wrapper.eq("city", user.getCity());
        }
        if(!StringUtils.isEmpty(user.getGender())){
            wrapper.eq("gender", user.getGender());
        }
        wrapper.orderByDesc("id");
        userService.page(page,wrapper);
        return Result.ok(page);
    }

    @ApiOperation("删除用户")
    @ApiImplicitParams(
        @ApiImplicitParam(name="id", value="用户id",dataType = "String",paramType = "path",required = true)
    )
    @DeleteMapping("/id/{id}")
    public  Result delete(@PathVariable String id){
        userService.removeById(id);
        return Result.ok();
    }
    @ApiOperation("修改用户")
    @PutMapping
    public Result update(@RequestBody User user){
        userService.updateById(user);
        return Result.ok();
    }

    @ApiOperation("添加用户")
    @PostMapping("/user")
    public Result addUser(@RequestBody User user){
        userService.saveOrUpdate(user);
        return Result.ok();
    }

    @ApiOperation("批量删除")
    @PostMapping("/delBatch")
    public Result delBatch(@RequestBody List<String> ids){
        userService.removeByIds(ids);
        return Result.ok();
    }
}

